package com.yunyi.web.controller.qiao;

import com.yunyi.common.annotation.Anonymous;
import com.yunyi.common.annotation.Log;
import com.yunyi.common.config.RuoYiConfig;
import com.yunyi.common.core.controller.BaseController;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.common.core.page.TableDataInfo;
import com.yunyi.common.enums.BusinessType;
import com.yunyi.common.utils.StringUtils;
import com.yunyi.common.utils.file.Base64FileUtil;
import com.yunyi.common.utils.file.FileUploadUtils;
import com.yunyi.common.utils.file.MimeTypeUtils;
import com.yunyi.common.utils.oss.AliyunOssFileUtils;
import com.yunyi.system.domain.Template;
import com.yunyi.system.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author stephen
 * @Date 2023/8/29 11:33
 * @ClassName: TemplateController
 * @Description: TODO
 * @Version 1.0
 */
@Anonymous
@RestController
@RequestMapping("/qiao/template")
@Api(tags = "模板信息控制器")
public class TemplateController extends BaseController {

    @Autowired
    private TemplateService templateService;

    @ApiOperation(value = "条件分页查询模板信息")
    @GetMapping("/list")
    public TableDataInfo list(Template template){
        System.out.println(template);
        startPage();
        List<Template> templates = templateService.selectTemplateList(template);
        return getDataTable(templates) ;
    }

    @ApiOperation(value = "根据编号id获取模板信息")
    @GetMapping("/{id}")
    public AjaxResult selectTempQueryId(@PathVariable("id") Long id){
        return success(templateService.selectTemplateById(id));
    }

    @ApiOperation(value = "新增模板信息")
    @PostMapping
    public AjaxResult insertTemp(@Validated @RequestBody Template template){
        if(StringUtils.isNotNull(template.getAddress())){
            try{
                MultipartFile file= Base64FileUtil.base64ToMultipart(template.getAddress());
                template.setAddress(AliyunOssFileUtils.uploadFile(file));
                System.out.println(template.getAddress());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        template.setCreateBy(getUsername());
        int i = templateService.insertTemp(template);
        return success(i);
    }


    @ApiOperation(value = "删除单条模板信息")
    @DeleteMapping("/id/{id}")
    public AjaxResult deleteTempById(@PathVariable("id") Long id){
        templateService.deleteTempById(id);
        return success();
    }

    @ApiOperation(value = "批量删除模板信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteTempByIds(@PathVariable("ids") Long[] ids){
        templateService.deleteTempByIds(ids);
        return success();
    }

    @ApiOperation(value = "修改模板信息")
    @PutMapping
    public AjaxResult updateTemp(@Validated @RequestBody Template template){
        if(StringUtils.isNotNull(template.getAddress())){
            try{
                MultipartFile file= Base64FileUtil.base64ToMultipart(template.getAddress());
                template.setAddress(AliyunOssFileUtils.uploadFile(file));
                System.out.println(template.getAddress());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        template.setUpdateBy(getUsername());
        int i = templateService.updateTemp(template);
        return success(i);
    }

    /**
     * 头像上传
     */
    @Log(title = "模板上传", businessType = BusinessType.UPDATE)
    @PostMapping("/tempAddress")
    public AjaxResult avatar(@RequestParam(value = "address") MultipartFile file) throws Exception
    {
        System.out.println(file);
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            System.out.println(avatar);
            return  success(avatar);
        }
        return error("上传图片异常，请联系管理员");
    }

}
